<?php
namespace Operation\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;

class BudgetController extends AbstractActionController
{
    protected $saleheadTable;
    protected $storeTable;
    
    public function setEventManager(EventManagerInterface $events)
    {
        parent::setEventManager($events);
 
        $controller = $this;
        $events->attach('dispatch', function ($e) use ($controller) {
            $controller->layout()->setTemplate('layout/operation');
        }, 100);
 
        return $this;
    }
    
    public function getSaleheadTable()
    {
        if (!$this->saleheadTable) {
            $sm = $this->getServiceLocator();
            $this->saleheadTable = $sm->get('Operation\Model\SaleheadTable');
        }
        return $this->saleheadTable;
    }
    
    public function getStoreTable()
    {
        if (!$this->storeTable) {
            $sm = $this->getServiceLocator();
            $this->storeTable = $sm->get('Operation\Model\StoreTable');
        }
        return $this->storeTable;
    }
    
    public function indexAction()
    {
        $request = $this->getRequest();
        if ($request->isPost()) {
            ///////////////////// PERIODE TEXT ///////////////////////
            $period = "";
            $periodM = "";
            $periodY = "";
            $periodJ = "";
            $monthText = $this->echoMonthText();
            $dayText = $this->echoShortDayText();
            
            if($request->getPost()->periodM){
                $i = 1;
                foreach($request->getPost()->periodM as $mois):
                    if($i < count($request->getPost()->periodM))
                        $periodM .= $monthText[$mois].", ";
                    else
                        $periodM .= $monthText[$mois];
                    $i++;
                endforeach;
            }
            if($request->getPost()->periodY){
                $i = 1;
                $periodY .= ' ';
                foreach($request->getPost()->periodY as $annee):                    
                    if($i < count($request->getPost()->periodY))
                        $periodY .= $annee.", ";
                    else
                        $periodY .= $annee;
                    $i++;
                endforeach;
            }
            if($request->getPost()->semaine){
                $i = 1;
                $periodJ .= ' - ';
                foreach($request->getPost()->semaine as $jour):                    
                    if($i < count($request->getPost()->semaine))
                        $periodJ .= $dayText[$jour]." ";
                    else
                        $periodJ .= $dayText[$jour];
                    $i++;
                endforeach;
            }
            
            $period = $periodM.$periodY.$periodJ;
            $period = ($period != "")?$period:"Toute la période";
            
            return new ViewModel(array(
                'monthText' => $monthText,

                ///////// PERIODE TEXT
                'periodText' => $period,
                
                ///////// STORE
                'store' => $this->getStoreTable()->getStore($request->getPost()->boutiques),
            )); 
        } else {
            return $this->redirect()->toRoute('operation/default');
        }        
    } 
    
    public function echoShortDayText()
    {
        $dayText[0] = 'dim';
        $dayText[1] = 'lun';
        $dayText[2] = 'mar';
        $dayText[3] = 'mer';
        $dayText[4] = 'jeu';
        $dayText[5] = 'ven';
        $dayText[6] = 'sam';        
        
        return $dayText;
    }
    
    public function echoLongDayText()
    {
        $dayText[0] = 'dimanche';
        $dayText[1] = 'lundi';
        $dayText[2] = 'mardi';
        $dayText[3] = 'mercredi';
        $dayText[4] = 'jeudi';
        $dayText[5] = 'vendredi';
        $dayText[6] = 'samedi';        
        
        return $dayText;
    }
    
    public function echoMonthText()
    {

        $monthText[1] = 'Janvier';
        $monthText[2] = 'Fevrier';
        $monthText[3] = 'Mars';
        $monthText[4] = 'Avril';
        $monthText[5] = 'Mai';
        $monthText[6] = 'Juin';
        $monthText[7] = 'Juillet';
        $monthText[8] = 'Août';
        $monthText[9] = 'Septembre';
        $monthText[10] = 'Octobre';
        $monthText[11] = 'Novembre';
        $monthText[12] = 'Décembre';
        
        return $monthText;
    }
}
?>
